<?php

class News extends Controller {
    
	function setVars()
	{
		//$this->tpl['tpljs'][]  = '';
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['title'] = "In The News";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = $this->tm->randomBlock();
        
        $this->offset = $this->uri->segment(3);
        
        $this->Alias = $this->session->userdata('Alias');
	}
	
	function __construct()
	{
		parent::Controller();
        //$this->load->library('profiler');
        $this->load->model('Tamer_Model', 'tm');
		$this->setVars();
		log_message('debug', "News: Controler Class Initialized");
	}
    
    
	function index()
	{
        $this->tpl['sidebar'] = $this->tm->randomBlock();
        $this->tpl['body'] = $this->liste();
        $this->load->view('blank', $this->tpl);
	}
    
	function liste()
	{
		$this->load->helper('text');
        $this->load->library('pagination');
        
        $this->total = $this->db->count_all('news');
        $limit = 5;
        
        $config['base_url'] = site_url("news/index");
        $config['total_rows'] = $this->total;
        $config['per_page'] = $limit;
        $config['uri_segment'] = 3;
        
        $this->pagination->initialize($config);
        
        $this->db->orderby('id', 'desc');
        
		$query = $this->db->get('news', $limit, $this->offset);
        
		$list = '';
		foreach($query->result() as $row){ 
			$list .= "<h3>$row->Title</h3>";
			$list .= "<p>" . word_limiter($row->Body, 15) ;
			$list .= anchor('news/article/'.$row->id, '→ View Article')."</p>";
			$list .= "<hr />";
		}
        
        $list .= $this->pagination->create_links();
        return $list;
	}


    
	function article()
	{
        if($this->offset === FALSE){
        	$total = $this->db->count_all('news');
            redirect('news/article/'.$total, 'location');
        }
        $this->load->helper('date');
        
        $format = 'r (T)';

        $this->tpl['sidebar'] .= $this->tm->recentNews();
        
        $this->db->where('id', $this->offset);
        $query_news = $this->db->get('news');
        $row = $query_news->row();
        //$this->fun->print_a($row);
        $this->tpl['head'] = '';
        $this->tpl['title'] = $row->Title;
        $this->tpl['body'] = $row->Body;
        $this->tpl['body'] .= '<p class="right">';
        $this->tpl['body'] .= '<span class="author">'.$row->Author.'</span><br />';
        $this->tpl['body'] .= '<span class="date">'.$this->timetamer->ldate($row->Time).'</span></p>';        
        $this->load->view('blank', $this->tpl);
	}
    
	function post()
	{
        $this->auth->CheckTaMeR(101);
		$this->tpl['Title'] = 'Post new Article';
        $this->tpl['Author'] = $this->Alias;
		$this->load->view('news/post', $this->tpl);
	}
    
	function post_insert()
	{
        $this->auth->CheckTaMeR(101,100);
		$this->load->library('validation');
		$rules['Title']="trim|required|min_length[4]|max_length[50]|xss_clean";
		$rules['Body']  ="trim|required|min_length[9]|max_length[4000]|xss_clean";
		$rules['Author']="required";
		
		$this->validation->set_rules($rules);
        
		if($this->validation->run() == FALSE)
		{
            $this->msg->setError('Validation error');
            $this->post();
		}
		else
		{
			$this->load->helper('date');
            $this->load->helper('typography');
			foreach($_POST as $k => $v)
			{
				$post[$k] = trim($v); 
			}
			//$post['Time'] = now(); // Converted db field to timestamp
            $post['Body'] = auto_typography($post['Body']);
			$this->db->insert('news', $post);
			redirect('news/article/'.$this->db->insert_id(), 'location');
		}
	}
	
	function comment_insert()
	{
		$this->load->library('validation');
		$rules['Body']  ="trim|required|min_length[15]|max_length[500]|xss_clean";
		$rules['Author']="required";
		
		$this->validation->set_rules($rules);
        
		if($this->validation->run() !== FALSE)
		{
			foreach($_POST as $k => $v)
			{
				$post[$k] = trim($v); 
			}
			$this->db->insert('comments', $post);
		}
		
		redirect('news/article/'.$_POST['mid']);
	}
}
?>
